Github is well known web service for source code management and distributed revision control. Github API provide various HTTP methods thrught which one can access details of a contribuotr, repo, organization etc.
Here I am using the same github API to know how much percentage a contributor has contributed to his repos, using number of commits. There are various wrapper libraries available in different languages to access this API, but here I am using only real github API and python request library to do the job.
import requests
username = raw_input("Enter github handler: ")
Enter github handler: exploreshaifali
Now first lets fetch all basic infro of the contributor and print them. For this we need to make an HTTP call to url "https://api.github.com/users/
def get_user_info(username):
""" Getting basic user infos: Step 1
username : u'login'
real name: u'name'
gravtar : u'avatar_url'
numbre of public repos
email: u'email'
github url: u'url'
"""
url = "https://api.github.com/users/"+username
r = requests.get(url).json()
print('username: {username} \n Real Name: {real_name} \n \
gravtar_url: {avatar_url} \n email address: {email} \n \
github url: {github_url} \n Number of Public repos: \
{public_repos}'.format(username=username, real_name=r[u'name'],
avatar_url= r[u'avatar_url'], email= r[u'email'], github_url=r[u'url'],
public_repos= r[u'public_repos']))
return r
user_info = get_user_info(username)
username: exploreshaifali Real Name: Shaifali Agrawal gravtar_url: https://avatars.githubusercontent.com/u/3762969?v=3 email address: agrawalshaifali09@gmail.com github url: https://api.github.com/users/exploreshaifali Number of Public repos: 5
Now lets fetch and print details of each public repo the contriboutor have
def get_repos_info(username, url):
'''
Step 2
url = url of all public repo list.
for each repo get:
name: u'name'
repo url :u'url', u'html_url', u'clone_url'
Number of contributors: u'contributors_url'
Number of total commits : https://api.github.com/repos/exploreshaifali/bonafidePro/commits
Number of commits made by given user
start date : u'created_at'
last updated date : u'updated_at'
number of issues, milestones
description: u'description'
'''
r = requests.get(url).json()
number_of_repo = len(r)
for i in range(number_of_repo):
_repo_name = r[i][u'name']
_repo_url = r[i][u'url']
_repo_description = r[i][u'description']
_number_of_contributors = get_number_of_contibutors_of_repo(r[i][u'contributors_url'])
_commits_url = _repo_url+'/commits'
_total_commits = get_total_number_of_commits(_commits_url)
_commits_by_given_user = get_number_of_commits_made_by_given_user(_commits_url, username)
_issues_url = _repo_url+'/issues'
_issues = get_total_issues_by_user_in_a_repo(username, _issues_url)
print('repo name: {name} \n repo url {repo_url} \n Description:{description}\
\n number of contributors: {number_of_contributors}\
\n total number of commits: {total_commits}\n Commits made by {username}:\
{number_of_commits_by_given_user}\n issues: {issues}\n'.format(name=_repo_name,
repo_url=_repo_url, number_of_contributors=_number_of_contributors,
total_commits=_total_commits,
description=_repo_description, username=username,
number_of_commits_by_given_user=_commits_by_given_user,
issues=_issues))
def get_number_of_contibutors_of_repo(url):
""" Step 2.b """
r = requests.get(url).json()
return len(r)
def get_total_number_of_commits(url):
""" Step 2.c """
r = requests.get(url).json()
return len(r)
def get_number_of_commits_made_by_given_user(url, username):
""" Step 2.d
url: repo commits api url
Return Number of commits made by a contributor in a particular repo.
"""
r = requests.get(url).json()
number_of_commits = 0
for i in range(len(r)):
'''
Though at present 'committer' key is used but many times committer and
author keys are none, so would be more better to get this info from 'commit' key. Need to
think more on how to get value using commit key.
'''
if r[i]['committer'] != None:
if r[i]['committer'][u'login'] == username:
number_of_commits += 1
return number_of_commits
#get issues for given repo and given username
def get_total_issues_by_user_in_a_repo(username, url):
""" return dict{'total_issues': value,
'issues_created_by_given_user': value}
:param username: github handler
:param url: url of repo
"""
r = requests.get(url).json()
total_issues = len(r)
issues_created_by_given_user = 0
total_open_issues=0
user_open_issues=0
for issue in r:
if issue[u'user'][u'login'] == username:
issues_created_by_given_user += 1
if issue[u'state'] == 'open':
user_open_issues += 1
if issue[u'state'] == 'open':
total_open_issues += 1
_dict = {'total_issues':total_issues, 'issues_created_by_given_user':issues_created_by_given_user,
'total_open_issues':total_open_issues, 'user_open_issues':user_open_issues}
return _dict
# get user's repo info:
get_repos_info(username, user_info[u'repos_url'])
print("Done!!!")
repo name: bonafidePro repo url https://api.github.com/repos/exploreshaifali/bonafidePro Description:To automate the process of issuing bonafide certificates in IIPS. number of contributors: 1 total number of commits: 9 Commits made by exploreshaifali: 8 issues: {'total_issues': 0, 'user_open_issues': 0, 'issues_created_by_given_user': 0, 'total_open_issues': 0} repo name: content repo url https://api.github.com/repos/exploreshaifali/content Description:Official content for Harvard CS109 number of contributors: 11 total number of commits: 30 Commits made by exploreshaifali: 0 issues: {'total_issues': 0, 'user_open_issues': 0, 'issues_created_by_given_user': 0, 'total_open_issues': 0} repo name: datasharing repo url https://api.github.com/repos/exploreshaifali/datasharing Description:The Leek group guide to data sharing number of contributors: 8 total number of commits: 25 Commits made by exploreshaifali: 0 issues: {'total_issues': 0, 'user_open_issues': 0, 'issues_created_by_given_user': 0, 'total_open_issues': 0} repo name: Poll-App repo url https://api.github.com/repos/exploreshaifali/Poll-App Description: number of contributors: 2 total number of commits: 30 Commits made by exploreshaifali: 30 issues: {'total_issues': 0, 'user_open_issues': 0, 'issues_created_by_given_user': 0, 'total_open_issues': 0} repo name: Programming repo url https://api.github.com/repos/exploreshaifali/Programming Description:Here I am putting my learning programs. number of contributors: 1 total number of commits: 26 Commits made by exploreshaifali: 26 issues: {'total_issues': 0, 'user_open_issues': 0, 'issues_created_by_given_user': 0, 'total_open_issues': 0}